#include "openmove_Prefix.h"
#include "SceneBase.h"
#include "OgreEngine.h"
#include "ExampleFrameListener.h"
#include "SkeletalAnimationFrameListener.h"
#include "OgreExternalTextureSourceManager.h"
#include "OgreExternalTextureSource.h"
#include "SceneTestParticule.h"
#include "SimpleFrameListener.h"

CSceneTestParticule :: CSceneTestParticule(COgreEngine &Creator, const String & Name, const int pos) : CSceneBase(Creator, Name, pos), m_pPluieTop(NULL)
{
}

CSceneTestParticule :: ~CSceneTestParticule(void)
{
}

#define NB_PARTICLE 5

char const * const g_Bones[NB_PARTICLE] =
{
"ATE_RightHand",
"ATE_LeftHand",
"ATE_RightFoot",
"ATE_head_End",
"ATE_Hips"
};

void CSceneTestParticule :: Create(TCreatorEntities const &CreatorEntities, TCreatorNodes const &CreatorNodes, TCreatorLights const &CreatorLights, ENetConfig IsClient, Vector3 &CameraStartPos)
{
	m_ShowEntitiesBBox = false;
	CSceneBase::Create(CreatorEntities, CreatorNodes, CreatorLights, IsClient, CameraStartPos);

	m_pCurrentCamera->setPosition(-180.8f, 87.7f, 452.3f);
	//Quaternion Quat(Radian(Degree(0.0f)), Vector3(0, 1, 0));
	//m_pCurrentCamera->setOrientation(Quat);
	m_pCurrentCamera->setDirection(0, 0, -1);
	m_OgreEngine.m_pViewPort->setBackgroundColour(ColourValue(1.0f, 0.0f, 0.9f, 1.0f));

	Entity *pLoup = NULL;
	for (TEntities::const_iterator It = m_Entities.begin(); It != m_Entities.end(); ++It)
	{
		if ((*It)->getName() == "TestParticuleglam-petitsloups")
		{
			pLoup = *It;
			break;
		}
	}

	AnimationState *pAnimationState = pLoup->getAnimationState("danse");
	SkeletonInstance *pSkeleton = pLoup->getSkeleton();

	for (int i = 0; i < NB_PARTICLE; ++i)
	{
		m_ppFlare[i] = m_OgreEngine.m_pCurrentSceneManager->createParticleSystem("Flare" + StringConverter::toString(i), "openmove/follow");
		SceneNode *pNode = m_pSceneNode->createChildSceneNode("Flare" + StringConverter::toString(i));
		pNode->attachObject(m_ppFlare[i]);

		if (NULL != pLoup)
		{
			m_pRightHand[i] = pSkeleton->getBone(g_Bones[i]);
		}
	}


	//m_OgreEngine.m_Root.addFrameListener(this);
	//m_FrameListeners.push_back(this);
}

void CSceneTestParticule :: Detach(void)
{
	CSceneBase::Detach();
}

void CSceneTestParticule :: Attach(void)
{
	CSceneBase::Attach();
}

bool CSceneTestParticule :: frameStarted(const FrameEvent& evt)
{
	CSceneBase::frameStarted(evt);
	for (int i = 0; i < NB_PARTICLE; ++i)
	{
		m_ppFlare[i]->getEmitter(0)->setPosition(m_pRightHand[i]->getWorldPosition());
	}

	return (true);
}